package com.yanqu.road.server.cmd.yrank.user;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.rank.yrank.UserYRank;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityInfoMgr;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuActivity;
import com.yanqu.road.server.manager.activity.biaoju.CrossBiaoJuMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = CrossProtocol.C2_CROSS_YRANK_GET_USER_RANK_TO_REWARD, desc = "")
public class CrossYRankGetUserRankToRewardCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        YRankProto.YRankReqMsg reqMsg = YRankProto.YRankReqMsg.parseFrom(packet.getBytes());
        Integer conditionType = Integer.valueOf(reqMsg.getIdKey());
        if (conditionType == eGamePlayerEventType.BiaoJuExpRank.getValue()) {
            CrossBiaoJuActivity activity = CrossBiaoJuMgr.getActivity(reqMsg.getActivityId(), reqMsg.getGroupId());
            if (activity != null) {
                activity.getUserRankReward(packet.getPlayerId(), serverId, reqMsg.getRewardConditionId());
            }
            return;
        }

        List<UserYRank> rankList = CrossYRankMgr.getSortUserRankList(reqMsg.getActivityId(), reqMsg.getGroupId(), reqMsg.getIdKey());
        int rankNum = CrossYRankMgr.getUserRankNum(rankList, packet.getPlayerId());

        ActivityInfo activityInfo = ActivityInfoMgr.getActivityInfo(activityId);
        int actvityType = 0;
        if(null != activityInfo){
            actvityType = activityInfo.getType();
        }
        if (actvityType == eActivityType.OreWarActivity.getValue()) {
            CrossActivityProto.CrossActivityGetRewardRespMsg.Builder respMsg = CrossActivityProto.CrossActivityGetRewardRespMsg.newBuilder();
            respMsg.setActivityId(activityId);
            respMsg.setConditionId(reqMsg.getRewardConditionId());
            respMsg.setRank(rankNum);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_ORE_WAR_GET_RANK_REWARD_NOTIFY, respMsg);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
            return;
        }

        CrossActivityProto.CrossActivityGetRewardRespMsg.Builder respMsg = CrossActivityProto.CrossActivityGetRewardRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setConditionId(reqMsg.getRewardConditionId());
        respMsg.setRank(rankNum);
        if (conditionType == eGamePlayerEventType.CrossTradeWarScoreRiseRank.getValue()){
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_GET_REWARD_NOTIFY, respMsg);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
            return;
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_ACTIVITY_GET_RANK_REWARD_NOTIFY, respMsg);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
    }

    @Override
    public void sendData() {

    }
}
